new Vue({
    el: '#app',
    data: {
        user: {
            name: '',
            idCard: ''
        },
        userCookie: {
            idCard: '',
            name: '',
        },
        rules: {
            name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
            idCard: [{ required: true, message: '请输入身份证', trigger: 'blur' }]
        },
        vshow: {
            showButton: true,
            loading: false,
            userShow: false,
            showMsg: false,
        },
        pagesize: 5,
        personTable: [],
        contactData: {
            contactType: '',
            grgl: '',

        },
        cxAreaTj: {//查询条件
            address: '',
            startTime: '',
            endTime: '',
        },
        cxTraffic:{
            trainNumber:'',
        },
        cxRiskAnalysis:'',
        pagesize: 5,
        areaDataTable: [],
        trafficDataTable:[],
        riskAnalysisDataTable:[],
        riskAnalysisDataforSearch:'',
        countNum:{
            trafficCount:'',
        }
    },
    methods: {
        searchMsg: function (formName) {
            var _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    $.ajax({
                        url: 'common/getUserMsg',
                        data: {
                            name: _this.user.name,
                            idCard: _this.user.idCard
                        },
                        type: 'post',
                        dataType: 'Json',
                        success: function (data) {
                            if (data.code == 1) {
                                if (data.grgl == '' || typeof (data.grgl) == "null") {
                                    _this.setCookie('grgl', 0, 1);
                                } else {
                                    _this.setCookie('grgl', data.grgl, 1);
                                    if (data.grgl > 0.5) {
                                        _this.$alert('根据系统判断，您属于感染高风险人群，请在家隔离', '高风险', {
                                            confirmButtonText: '确定'
                                        });
                                    }
                                }
                                _this.setCookie('idCard', _this.user.idCard, 1);
                                _this.setCookie('name', _this.user.name, 1);
                                _this.vshow.userShow = true;
                                _this.userCookie.idCard = _this.getCookie('idCard');
                                _this.userCookie.name = _this.getCookie('name');
                                _this.userCookie.grgl = data.grgl;
                                _this.searchContactor();
                                _this.vshow.showButton = false;
                                _this.vshow.showMsg = true;
                            } else if (data.code == 2) {
                                _this.$message.error('身份证已存在，与姓名不符');
                            } else {
                                _this.$message.error('系统异常');
                            }
                        }, error: function (data) {
                            _this.$message.error('系统异常');
                        }
                    });


                    // _this.vshow.loading = true;
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        searchContactor: function () {
            var _this = this;
            $.ajax({
                url: 'common/getContactor',
                data: {
                    idCard: _this.userCookie.idCard
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    for (var i = data.length - 2; i >= 0; i--) {
                        var contactname;
                        if (data[i].contactName == 'kiss') {
                            contactname = "亲吻及以上"
                        } else if (data[i].contactName == 'talk') {
                            contactname = "面对面交谈"
                        } else if (data[i].contactName == 'handshake') {
                            contactname = "握手"
                        } else if (data[i].contactName == 'embrace') {
                            contactname = "拥抱"
                        } else {
                            contactname = '无亲密接触'
                        }
                        var personObject = {
                            contactType: data[i].contactType,
                            name: data[i].name,
                            address: data[i].address,
                            grgl: data[i].grgl,
                            contactTime: data[i].contactTime,
                            contactName: contactname,
                        };
                        _this.personTable.push(personObject);
                    }
                }, error: function (data) {

                }
            });
        },
        //设置cookie
        setCookie: function (cname, cvalue, exdays = 0) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            console.info(cname + "=" + cvalue + "; " + expires);
            document.cookie = cname + "=" + cvalue + "; " + expires;
            console.info(document.cookie);
        },
        //获取cookie
        getCookie: function (cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            console.log("获取cookie,现在循环")
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                console.log(c)
                while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        },
        //清除cookie
        clearCookie: function (name) {
            this.setCookie(name, "", -1);
        },
        clearUser: function () {
            this.clearCookie('idCard');
            this.clearCookie('name');
            this.clearCookie('grgl');
            this.QINGKONGDUIXIANGZHI(this.userCookie);
            this.QINGKONGDUIXIANGZHI(this.user);
            this.vshow.userShow = false;
            this.vshow.showButton = true;
            this.vshow.showMsg = false;
            this.personTable = [];
        },
        handleCurrentChange: function (currentPage) {
            var _this = this;
            _this.cxPerson((currentPage - 1) * _this.pagesize, _this.pagesize);
        },
        pageInit: function () {
            var _this = this;
            _this.userCookie.idCard = _this.getCookie("idCard");
            _this.userCookie.name = _this.getCookie("name");
            _this.userCookie.grgl = _this.getCookie("grgl");
            if (_this.userCookie.grgl > 0.5) {
                _this.$alert('根据系统判断，您属于感染高风险人群，请在家隔离', '高风险', {
                    confirmButtonText: '确定'
                });
            }
        },
        QINGKONGDUIXIANGZHI: function (obj) {
            for (var key in obj) {
                obj[key] = '';
            }
        },
        selectPage: function (key, keyPath) {
            $(window).attr('location', keyPath);
        },
        //折线图啥的
        kshHuiZhi: function () {
            //初始化ehcharts实例
            var myChart = echarts.init(document.getElementById("box"));
            var _this = this;
            $.ajax({
                url: 'common/getTj',
                data: {
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    console.log(data)
                    //指定图表的配置项和数据
                    var timelist = [];
                    var qz = [];
                    var gfx = [];
                    var zy = [];
                    for (var i = 0; i < data.length; i++) {
                        timelist.push(data[i].time)
                        qz.push(data[i].qz)
                        gfx.push(data[i].gfx)
                        zy.push(data[i].zy)
                    }
                    var option = {
                        //标题
                        title: {
                            text: '疫情情况统计'
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'cross',
                                label: {
                                    backgroundColor: '#6a7985'
                                }
                            }
                        },
                        //工具箱
                        //保存图片
                        toolbox: {
                            show: true,
                            feature: {
                                saveAsImage: {
                                    show: true
                                }
                            }
                        },
                        //图例-每一条数据的名字叫销量
                        legend: {
                            data: ["确诊人数", "高风险人数", "治愈人数"]
                        },
                        //x轴
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: timelist
                        },
                        //y轴没有显式设置，根据值自动生成y轴
                        yAxis: {},
                        //数据-data是最终要显示的数据
                        series: [{
                            name: '确诊人数',
                            type: 'line',
                            data: qz
                        }, {
                            name: '高风险人数',
                            type: 'line',
                            data: gfx
                        }, {
                            name: '治愈人数',
                            type: 'line',
                            data: zy
                        }]
                    };
                    //使用刚刚指定的配置项和数据项显示图表
                    myChart.setOption(option);
                }, error: function (data) {

                }
            });

        },
        getAreaData: function () {
            var _this = this;
            var address = _this.cxAreaTj.address.split('|');
            $.ajax({
                url: 'common/getAreaData',
                data: {
                    address: address[0],
                    startTime: _this.cxAreaTj.startTime,
                    endTime: _this.cxAreaTj.endTime,
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    _this.areaDataTable=[];
                    for (var i = data.length - 1; i >= 0; i--) {
                        _this.areaDataTable.push({
                            xqAddress: data[i].xqAddress,
                            addressName: data[i].addressName,
                            reachTime: data[i].reachTime,
                            leaveTime: data[i].leaveTime,
                        })
                    }
                }, error: function () {

                }
            });
        },
        getAddress: function (province, city, area) {
            var _this = this;
            $.ajax(
                {
                    url: 'http://api.map.baidu.com/location/ip',
                    data: {
                        coor: 'bd09ll',
                        ak: '3tnSxTxwk3wv5tm7F1N3upqd5nMDqBcl',
                        ip: ''
                    },
                    type: 'get',
                    dataType: 'jsonp',
                    async: true,
                    success: function (data) {
                        if(data.content.address_detail.province == data.content.address_detail.city){
                            _this.cxAreaTj.address = data.content.address_detail.province;
                        }else {
                            _this.cxAreaTj.address = data.content.address_detail.province + '|'+ data.content.address_detail.city;
                        }
                        
                        _this.getAreaData();
                    },
                    error: function (data) {
                        alert('获取地址失败');
                    }
                }
            );
        },
        cxAreaChange:function(){
            var _this = this;
            _this.getAreaData();
        },
        getTraffic:function(skipNum = 0, limitNum = 5){
            var _this = this;
            $.ajax({
                url: 'common/getTrafficData',
                data: {
                    trainNumber: _this.cxTraffic.trainNumber,
                    skip:skipNum,
                    limit:limitNum
                },
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    _this.trafficDataTable=[];
                    _this.countNum.trafficCount = data[data.length - 1].trafficCount;
                    for (var i = data.length - 2; i >= 0; i--) {
                        _this.trafficDataTable.push({
                            trainNumber: data[i].trainNumber,
                            startCity: data[i].startCity,
                            endCity: data[i].endCity,
                            startStation: data[i].startStation,
                            endStation: data[i].endStation,
                            startTime: data[i].startTime,
                            endTime: data[i].endTime
                        });
                    }
                }, error: function () {

                }
            });
        },
        handleTrafficCurrentChange: function (currentPage) {
            var _this = this;
            _this.getTraffic((currentPage - 1) * _this.pagesize, _this.pagesize);
        },
        cxTrafficChange:function(){
            var _this = this;
            _this.getTraffic();
        },
        riskAnalysis:function(){
            var _this = this;
            _this.riskAnalysisDataTable = [];
            $.ajax({
                url:'common/riskAnalysis',
                data:{},
                type: 'post',
                dataType: 'Json',
                success: function (data) {
                    _this.riskAnalysisDataforSearch = data;
                    for(var i = data.length - 1; i >= 0; i--){
                        _this.riskAnalysisDataTable.push(data[i]);
                    }
                },error:function(){

                }
            });
        },
        cxRiskAnalysisChange:function(){
            var _this = this;
            _this.riskAnalysisDataTable=[];
            if(_this.cxRiskAnalysis==''){
                for(var i = _this.riskAnalysisDataforSearch.length - 1; i >= 0; i--){
                    _this.riskAnalysisDataTable.push(_this.riskAnalysisDataforSearch[i]);
                }
            } else {
                for(var i = _this.riskAnalysisDataforSearch.length - 1; i >= 0; i--){
                    //如果字符串中不包含目标字符会返回-1
                    if(_this.riskAnalysisDataforSearch[i].province.indexOf(_this.cxRiskAnalysis)>=0 
                    || _this.riskAnalysisDataforSearch[i].city.indexOf(_this.cxRiskAnalysis)>=0 
                    || _this.riskAnalysisDataforSearch[i].area.indexOf(_this.cxRiskAnalysis)>=0){
                        _this.riskAnalysisDataTable.push(_this.riskAnalysisDataforSearch[i]);
                    }         
                }
            }
        },
            
    },
    computed: {

    },
    mounted() {
        var _this = this;
        _this.getAddress();
        _this.pageInit();
        _this.kshHuiZhi();
        _this.getTraffic();
        _this.riskAnalysis();
        if (_this.getCookie('idCard') !== '') {
            _this.vshow.userShow = true;
            _this.vshow.showButton = false;
            _this.vshow.showMsg = true;
            _this.searchContactor();
        }
    },
})